package com.example.wasaimovie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.LeavingMsg;
import com.example.wasaimovie.Model.entity.Worker;
import com.example.wasaimovie.Model.entity.WorkerEvaluate;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.LeavingMsgService;
import com.example.wasaimovie.Service.WorkerEvaluateService;
import com.example.wasaimovie.Service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.Loader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api")
@Slf4j
public class WorkerController {

    @Autowired
    WorkerService workerService;

    @Autowired
    LeavingMsgService leavingMsgService;

    @Autowired
    WorkerEvaluateService workerEvaluateService;

    @Value("${movie.image.url.path}")
    String url;

    /**
     * 获取所有客服信息
     * @return 客服详情
     */
    @CrossOrigin
    @GetMapping("/worker")
    public List<Worker> getWorkerList(){
        List<Worker> result = workerService.list();
        for (Worker worker : result) {
            worker.setAvatar(url + worker.getAvatar());
        }
        return result;
    }

    /**
     * 获取电影院留言
     * @return 留言信息
     */
    @CrossOrigin
    @GetMapping("/lm")
    public List<Map<String, Object>> getLeavingMsg(){
        List<Map<String, Object>> result = leavingMsgService.getLeavingMsgAndUser();;

        return result;
    }

    /**
     * 发表影院留言
     * @param leavingMsg 留言信息
     * @return  是否成功
     */
    @CrossOrigin
    @PostMapping("/lm")
    @DisableBaseResponse
    public ResponseResult<Object> LeavingMsg(@RequestBody LeavingMsg leavingMsg){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        leavingMsg.setId(UUID.randomUUID().toString());
        leavingMsg.setCreateTime(format.format(new Date()));
        boolean save = leavingMsgService.save(leavingMsg);
        if (save){
            return new ResponseResult<>(200);
        }
        return new ResponseResult<>(500, "评价失败");
    }

    /**
     * 获取该用户评价
     * @param id 客服id
     * @return 该客服所有评价
     */
    @CrossOrigin
    @GetMapping("/we/{id}")
    public List<WorkerEvaluate> getWorkerEvaluete(@PathVariable String id){
        QueryWrapper<WorkerEvaluate> qw = new QueryWrapper<>();
        qw.eq("wid", id);
        List<WorkerEvaluate> result = workerEvaluateService.list(qw);
        return result;
    }

    /**
     * 评价客服
     * @param workerEvaluate 评价信息
     * @return 是否成功
     */
    @CrossOrigin
    @PostMapping("/we")
    @DisableBaseResponse
    public ResponseResult<Object> evaluate(@RequestBody WorkerEvaluate workerEvaluate){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        workerEvaluate.setId(UUID.randomUUID().toString());
        workerEvaluate.setCreateTime(format.format(new Date()));
        System.out.println(workerEvaluate);
        boolean save = workerEvaluateService.save(workerEvaluate);
        if (save){
            return  new ResponseResult<>(200);
        }
        return new ResponseResult<>(500, "添加失败");
    }

    /**
     * 修改员工信息
     * @param worker 员工信息
     * @return 是否修改成功
     */
    @PutMapping("/worker")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> updateWorker(@RequestBody Worker worker){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        worker.setAvatar(worker.getAvatar().substring(worker.getAvatar().lastIndexOf('/') + 1));
        worker.setUpdateTime(format.format(new Date()));
        if (workerService.updateById(worker)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败");
    }

    /**
     * 删除员工
     * @param id 员工id
     * @return 是否删除成功
     */
    @DeleteMapping("/worker/{id}")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> deleteWorker(@PathVariable String id){
        if (workerService.removeById(id)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "删除失败");
    }

    @PostMapping("/worker")
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> addWorker(@RequestBody Worker worker){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        worker.setCreateTime(format.format(new Date()));
        worker.setUpdateTime(format.format(new Date()));
        worker.setId(UUID.randomUUID().toString());
        if ("".equals(worker.getAvatar())){
            worker.setAvatar("6dcbec9e-5166-4e87-964e-bc9c5853a3d9");
        }else {
            worker.setAvatar(worker.getAvatar().substring(worker.getAvatar().lastIndexOf('/') + 1));
        }
        if (workerService.save(worker)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "添加失败");
    }
}
